import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/view/material/material_list_widget.dart';
import 'package:loxen/view/material/material_tabbar.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/viewmode/material_category_viewmodel.dart';
import 'package:loxen/viewmode/search_result_viewmodel.dart';
import 'package:loxen/widget/empty_widget.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:loxen/widget/my_subassembly.dart';
import 'package:provider/provider.dart';

class MaterialCategoryWidget extends StatelessWidget {
  final String category;

  const MaterialCategoryWidget({Key key, this.category}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<MaterialCategoryViewModel>(
      create: (_) {
        MaterialCategoryViewModel model = MaterialCategoryViewModel();
        model.loadData(category);
        return model;
      },
      child: Scaffold(
        appBar: PreferredSize(
          preferredSize: Size(double.infinity, 95.h),
          child: Container(
            color: Theme.of(context).backgroundColor,
            child: SafeArea(
              child: Row(
                children: <Widget>[
                  MySubassembly.getBackIcon(context,
                      color: Theme.of(context).textTheme.subtitle2.color),
                  Expanded(
                      child: Selector<MaterialCategoryViewModel, BaseState>(
                          builder: (_, state, __) {
                            if (state == BaseState.CONTENT) {
                              return MaterialTabBar();
                            } else {
                              return Text('');
                            }
                          },
                          selector: (_, model) => model.mState)),
                  MySubassembly.getSearchIcon(() {
                    Navigator.of(context).pushNamed(Routes.search,
                        arguments: {'mType': SearchType.MATERIAL, 'hintText': '输入素材名称'});
                  }, url: 'images/material_search.png')
                ],
              ),
            ),
          ),
        ),
        body: Selector<MaterialCategoryViewModel, BaseState>(
            builder: (context, state, __) {
              if (state == BaseState.LOADING) {
                return LoadingWidget();
              }
              if (state == BaseState.FAIL) {
                return FailWidget();
              }
              if (state == BaseState.EMPTY) {
                return EmptyWidget();
              }
              if (state == BaseState.CONTENT) {
                return Padding(
                  padding: EdgeInsets.only(top: 25.h, right: 34.w, left: 34.w),
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.only(bottom: 40.h),
                        child: MaterialTabBar2(),
                      ),
                      Expanded(child: MaterialListWidget())
                    ],
                  ),
                );
              }
              return Text('');
            },
            selector: (_, model) => model.mState),
      ),
    );
  }
}
